<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" >
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
    <el-form-item label="维修编码" prop="code">
      <el-input v-model="dataForm.code" placeholder="维修编码"></el-input>
    </el-form-item>
    <el-form-item label="维修名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="维修名称"></el-input>
    </el-form-item>
    <el-form-item label="维修费用" prop="price">
      <el-input v-model="dataForm.price" placeholder="维修费用"></el-input>
    </el-form-item>
    <el-form-item label="维修类别" prop="type">
      <el-select v-model="dataForm.type" placeholder="请选择">
        <el-option label="外部" value="1"></el-option>
        <el-option label="内部" value="2"></el-option>
      </el-select>
      <!-- <el-input v-model="dataForm.type" placeholder="维修类别(1-外部,2-内部)"></el-input> -->
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getRepairObj, addRepairObj, putRepairObj} from '@/api/eims/repair-config.js'

    export default {
    data () {
      return {
				ischeck: false,
        visible: false,
        dataForm: {
          id: 0,
          code: '',
          name: '',
          price: '',
          type: '1'
        },
        dataRule: {
          code: [
            { required: true, message: '编码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '费用不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '维修类别(1-外部,2-内部)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getRepairObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
				this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putRepairObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false 
              }).catch(() => {
                this.ischeck = false
              })
            } else {
              addRepairObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false 
              }).catch(() => {
                this.ischeck = false
              })
            }
          }else{
						this.ischeck = false
					}
        })
      }
    }
  }
</script>
